/*
 * 描          述:  <描述>
 * 修  改   人:  Administrator
 * 修改时间:  2019年4月30日
 * <修改描述:>
 */
package com.tx.core.starter.component;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;

import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration;
import com.tx.core.mybatis.support.MyBatisDaoSupport;

/**
 * 基础数据持久层配置逻辑<br/>
 * <功能详细描述>
 * 
 * @author  Administrator
 * @version  [版本号, 2019年4月30日]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
@org.springframework.context.annotation.Configuration
@ConditionalOnBean(SqlSessionFactory.class)
@EnableConfigurationProperties(ComponentProperties.class)
@AutoConfigureAfter(MybatisPlusAutoConfiguration.class)
@Import({ TableInitializerExecutor.class })
public class ComponentSupportAutoConfiguration implements InitializingBean {
    
    /** mybatis属性 */
    private SqlSessionFactory sqlSessionFactory;
    
    /**
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        //
    }
    
    /** <默认构造函数> */
    public ComponentSupportAutoConfiguration(
            SqlSessionFactory sqlSessionFactory) {
        super();
        
        this.sqlSessionFactory = sqlSessionFactory;
    }
    
    /**
     * 构造sqlSessionTemplate
     * <功能详细描述>
     * @return
     * @throws Exception [参数说明]
     * 
     * @return SqlSessionTemplate [返回类型说明]
     * @exception throws [异常类型] [异常说明]
     * @see [类、类#方法、类#成员]
     */
    @Bean({ "tx.component.sqlSessionTemplate", "sqlSessionTemplate" })
    @ConditionalOnMissingBean(name = "tx.component.sqlSessionTemplate")
    public SqlSessionTemplate sqlSessionTemplate() throws Exception {
        return new SqlSessionTemplate(this.sqlSessionFactory);
    }
    
    /**
     * 注册myBatisDaoSupport类<br/>
     * <功能详细描述>
     * @param sqlSessionFactory
     * @return [参数说明]
     * 
     * @return MyBatisDaoSupport [返回类型说明]
     * @exception throws [异常类型] [异常说明]
     * @see [类、类#方法、类#成员]
     */
    @Bean({ "tx.component.myBatisDaoSupport", "myBatisDaoSupport" })
    @ConditionalOnMissingBean(name = "tx.component.myBatisDaoSupport")
    public MyBatisDaoSupport myBatisDaoSupport() throws Exception {
        MyBatisDaoSupport myBatisDaoSupport = new MyBatisDaoSupport(
                sqlSessionTemplate());
        return myBatisDaoSupport;
    }
}
